package com.lisx.module_note.activity;

import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_data.constants.EventBusConstants;
import com.fenghuajueli.lib_data.entity.db.NoteDbEntity;
import com.fenghuajueli.lib_data.entity.eventbus.EventEntity;
import com.fenghuajueli.libbasecoreui.manager.NoteManager;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.lisx.module_note.R;
import com.lisx.module_note.databinding.ActivityWriteNoteBinding;
import com.lisx.module_note.dialogfragment.SelectDialogFragment;
import com.lisx.module_note.util.TimeUtils;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class WriteNoteActivity extends BaseActivity implements View.OnClickListener {

    private ActivityWriteNoteBinding binding;
    private NoteDbEntity entity;
    private NoteDbEntity noteDbEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWriteNoteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        noteDbEntity = getIntent().getParcelableExtra("note");

        binding.ivFanhui.setOnClickListener(this);
        binding.ivMood.setOnClickListener(this);
        binding.ivWeather.setOnClickListener(this);
        binding.tvComplete.setOnClickListener(this);
        entity = new NoteDbEntity();

        if (noteDbEntity != null) {
            binding.tvTime.setText(TimeUtils.parseDefaultTimeLong(noteDbEntity.getRecord_time() / 1000));
            binding.etMsg.setText(noteDbEntity.getMsg());
            binding.tvWeek.setText(TimeUtils.getWeek(noteDbEntity.getRecord_time()));
            if (noteDbEntity.getMood_res() != 0) {
                binding.ivMood.setImageResource(noteDbEntity.getMood_res());
            }
            if (noteDbEntity.getWeather_res() != 0) {
                binding.ivWeather.setImageResource(noteDbEntity.getWeather_res());
            }
        }
    }

    private void saveNote() {
        String msg = binding.etMsg.getText().toString().trim();
        if (TextUtils.isEmpty(msg)) {
            ToastUtils.showShort("请输入日记内容!");
            return;
        }
        entity.setCreate_time(System.currentTimeMillis());
        entity.setMsg(msg);
        entity.setRecord_time(System.currentTimeMillis());
        if (noteDbEntity != null) {
            noteDbEntity.setRecord_time(System.currentTimeMillis());
            noteDbEntity.setMsg(msg);
        }
        Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Boolean> emitter) throws Exception {
                if (noteDbEntity == null) {
                    NoteManager.getInstance().saveNoteDbEntity(entity);
                } else {
                    NoteManager.getInstance().saveNoteDbEntity(noteDbEntity);
                }
                emitter.onNext(true);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Boolean aBoolean) {
                        EventBus.getDefault().post(new EventEntity(EventBusConstants.UPDATE_NOTE));
                        if (noteDbEntity == null) {
                            ToastUtils.showShort("添加成功");
                        } else {
                            ToastUtils.showShort("修改成功");
                        }
                        finish();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        if (noteDbEntity == null) {
                            ToastUtils.showShort("添加失败");
                        } else {
                            ToastUtils.showShort("修改失败");
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_fanhui) {
            finish();
        } else if (v.getId() == R.id.iv_mood || v.getId() == R.id.iv_weather) {
            getIcon();
        } else if (v.getId() == R.id.tv_complete) {
            saveNote();
        }
    }

    private void getIcon() {
        SelectDialogFragment fragment = new SelectDialogFragment();
        fragment.setOnSelectIconListener(new SelectDialogFragment.OnSelectIconListener() {
            @Override
            public void onSelectIcon(int moodRes, int weatherRes) {
                entity.setMood_res(moodRes);
                entity.setWeather_res(weatherRes);
                if (moodRes != 0) {
                    binding.ivMood.setImageResource(moodRes);
                    if (noteDbEntity != null) {
                        noteDbEntity.setMood_res(moodRes);
                    }
                }
                if (weatherRes != 0) {
                    binding.ivWeather.setImageResource(weatherRes);
                    if (noteDbEntity != null) {
                        noteDbEntity.setWeather_res(weatherRes);
                    }
                }

            }
        });
        fragment.show(getSupportFragmentManager());
    }
}
